﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MoleculeViewer.DAO;
using SpringGraph;
using MoleculeViewer.Entities;
using System.IO;

namespace MoleculeViewer
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            ListBoxMolecules.ItemsSource = new List<string>()
            {
                "accholine",
                "adenosine",
                "adrenalin",
                "dopamine",
                "glutamate"
            };
            ListBoxMolecules.SelectedIndex = 0;
        }

        private void ListBoxMolecules_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listbox = sender as ListBox;

            if (listbox.SelectedItem != null)
            {
                MoleculeDAO dao = new MoleculeDAO();
                dao.GetMoleculeCompleted += new EventHandler<GetMoleculeEventArgs>(dao_GetMoleculeCompleted);
                dao.GetMolecule(listbox.SelectedItem.ToString());
            }
        }

        void dao_GetMoleculeCompleted(object sender, GetMoleculeEventArgs e)
        {
            Molecule molecule = e.Molecule;

            MoleculeViewer.SetMolecule(molecule);
        }

        private void ButtonLoadFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            var result = dialog.ShowDialog();
            if ((bool)result)
            {
                using (Stream stream = dialog.File.OpenRead())
                {
                    MoleculeViewer.SetMolecule(stream);
                }
            }
        }

    }
}
